package smecta.client.print.util;

import smecta.client.print.common.exceptions.ServiceException;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class DecodeUtil {
    public static final String PRIVATE_Key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDdzPGPBbtJ/YcgvBtq9Co2szOfuMwSEb2/dekmwa54XrdBHGeGAF4Ic5Tv7Sh5FSHdA7Zu5f/A7MwojEkJ+yrDdRAfWUDEBqwPhg9EeH0lImqIs2QGMlkBLTXKk/KrvPlIWyb+fDTcT98OGDTgwsstF5SHbBhrVbJtgcs89jrneWYqufLaNBgalz2TQyjnZEr6gbwK5a6oHVmsVWnrm0XgTXSl8xm1CJjiQrZ8z8scbeCfhgY2NgxA3APw+YpGQ0zm4uGqKTDtRKqDC3ak59CvM+a9jLxQeju2JEipx/EhdmthhE0ac1QniEvoHW8RQZkj4TyP7fzLtgPAt+YqXHAjAgMBAAECggEAQrXzC5jmOX1jKLHbL0KL9dLVWptq6Z5ja1QsEYeckcCvLoq8Cl5O6gzVhYpXq39zJXYJ53YPA/hU+XckOrfMJ2istjkSlUMKekrQicARp9sfnskR8xkukv+Fl84C5aubcKtV4aH7Bi6RwLwAH4AHOyNF/atB9vqaBXzN46KNmfPmlNojP155CEpTECOcjSxNQDq7RzBtaYZKBvx7qr9Zj1uAkEGnmp/WzZMiiMfwBl5XhVqxVmnPw+bPxp3JJTDfQI94SFjCHF7HwFmr2UY6eGpNCS/DzJt7dntplPMpbdrwB35Uifx6jXZdeYcyl+h95kogsv9A7tycvwP1SlS11QKBgQDk1kfT7rxd/LddfsZelghLqOStQno0BI07qH0FglAbWeqBV3FLDz7+yjllzFyaEBogIzvXrUtPTyAxZg06PBRsLXw8QQTm6NcvYAmwG9tgq/YA8+TMcspcSnzUU+n4KZqdYybZE06nNC2Ccu3OBncjc4GWUPc3OfwzRmeSColwpQKBgQD4INgfQDoJ1glxdffbG1PSoqyafskSLq5F7z2gZ0EfdqofQKI9CVEWOmAvpODsloBQGn4IAACgO6TGKqqm2PRRknZHR+TEwk3USwxER41rDLS+02soCbKMFnPIkQ3BKU9FJ0OWqTmd/xb7GMW6jEReL0kcKbRMBOqekNZx5ol7JwKBgEhtxig/LOG2pjbydEjlxdRkrZRlMRabxjP2vTNsLnyptf6gNZyjQ7YTPZ2J1Fly9UHsxPkbwXVlvvPDsDYZZAFlWCgeEDYhxlIZXJegCYyMgP2GNIm9X21cqivuL2S7jlN4WYhlgwSLEEh0pe4Kv9S5r29rSwPw13FHPGrN6Ph9AoGBAOo09krpMN8/7MHv+/gRH1neutVJfzluXokcfSr+y3WLaKD/PSQnuyYkC85s7vHiTVNO7k9W0UBti+aRN2sttc+ZU5OMxN8EGLqd7AI3F8FHyQOmQ6sTHDkmqDtUz4rYFz7LBP0c1onP0j1I8LpY09+i+nte0hQ/P8ZUKFZyqKpZAoGBAN9yie4+dtXBkc17xBdE+CUyAXtRjTYGRb6DltTWJZfNwDBEbU+ghaikFRLPYZJNt9QzD097GiPNAD0fyPq09cnNbdOKIhstPw4kv5fX9/FusYEsu/QSMtZN/ma3+WKU2oQlxKh+moKoOr0hX3aDjksVSDNMrsJdasjMGdoxsIQR";

    public static String decrypt(String encryptedData, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decodedData = Base64.getDecoder().decode(encryptedData);
            byte[] decryptedData = cipher.doFinal(decodedData);
            return new String(decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PrivateKey generatePrivateKey(String privateKeyString) {
        try {
            // 假设privateKeyString是Base64编码的私钥字符串
            // 首先，将Base64编码的字符串解码为字节数组
            byte[] keyBytes = Base64.getDecoder().decode(privateKeyString);

            // 使用PKCS8EncodedKeySpec处理PKCS#8格式的私钥
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

            // 获得KeyFactory实例（指定RSA算法）
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            // 使用KeyFactory从编码的私钥规范生成私钥
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

            return privateKey;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
